
#import "ServerDetailsViewController.h"

@implementation ServerDetailsViewController

//
// server info
//
@synthesize serverInfo														= _serverInfo;

//
// callback
//
@synthesize callback														= _callback;

//
// init with server info
//
- (id)initWithServerInfo:(ServerInfo*)serverInfo
{
	//
	// call super
	//
	self																	= [super initWithStyle:UITableViewStyleGrouped];
	if(!self)
		return nil;
	
	//
	// setup button
	//
	self.title																= NSLocalizedStringFromTable(@"Server Details", @"ServerDetailsViewController", nil);
	self.navigationItem.rightBarButtonItem									= [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(commitEdit:)] autorelease];
	self.navigationItem.leftBarButtonItem									= [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelEdit:)] autorelease];
	self.serverInfo															= serverInfo;
	if(!self.serverInfo)
	{
		_serverInfo															= [[ServerInfo alloc] init];
		self.serverInfo.serverType											= @"rtorrent-xmlrpc";
	}
	
	return self;
}

//
// dealloc
//
- (void)dealloc
{
	self.serverInfo															= nil;
	self.callback															= nil;
	
	[super dealloc];
}

//
// get section count
//
- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
{
	return 1;
}

//
// get row count
//
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
	return 5;
}

//
// get cell
//
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
	UITableViewCell* theCell												= [tableView dequeueReusableCellWithIdentifier:@"v1"];
	if(!theCell)
		theCell																= [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"v1"] autorelease];
	
	UITextField* textField													= [[[UITextField alloc] initWithFrame:CGRectMake(0, 0, 180, 21)] autorelease];
	switch(indexPath.row)
	{
		//
		// server name
		//
	case 0:
		theCell.textLabel.text												= NSLocalizedStringFromTable(@"Name", @"ServerDetailsViewController", nil);
		textField.placeholder												= NSLocalizedStringFromTable(@"server name", @"ServerDetailsViewController", nil);
		textField.text														= self.serverInfo.serverName;
		textField.tag														= 0;
		textField.keyboardType												= UIKeyboardTypeDefault;
		textField.returnKeyType												= UIReturnKeyNext;
		break;
		
		//
		// server type
		//
	case 1:
		theCell.textLabel.text												= NSLocalizedStringFromTable(@"Type", @"ServerDetailsViewController", nil);
		textField.placeholder												= NSLocalizedStringFromTable(@"server type", @"ServerDetailsViewController", nil);
		textField.text														= self.serverInfo.serverType;
		textField.tag														= 1;
		break;
		
		//
		// server address
		//
	case 2:
		theCell.textLabel.text												= NSLocalizedStringFromTable(@"Address", @"ServerDetailsViewController", nil);
		textField.placeholder												= NSLocalizedStringFromTable(@"server address", @"ServerDetailsViewController", nil);
		textField.text														= self.serverInfo.serverAddress;
		textField.tag														= 2;
		textField.keyboardType												= UIKeyboardTypeURL;
		textField.returnKeyType												= UIReturnKeyNext;
		break;
			
		//
		// user name
		//
	case 3:
		theCell.textLabel.text												= NSLocalizedStringFromTable(@"AuthUser", @"ServerDetailsViewController", nil);
		textField.placeholder												= NSLocalizedString(@"optional", nil);
		textField.text														= self.serverInfo.userName;
		textField.tag														= 3;
		textField.keyboardType												= UIKeyboardTypeDefault;
		textField.returnKeyType												= UIReturnKeyNext;
		break;
		
		//
		// password
		//
	case 4:
		theCell.textLabel.text												= NSLocalizedStringFromTable(@"AuthPass", @"ServerDetailsViewController", nil);
		textField.placeholder												= NSLocalizedString(@"optional", nil);
		textField.text														= self.serverInfo.password;
		textField.tag														= 4;
		textField.keyboardType												= UIKeyboardTypeASCIICapable;
		textField.returnKeyType												= UIReturnKeyDone;
		textField.secureTextEntry											= YES;
		break;
	}
	
	textField.adjustsFontSizeToFitWidth										= YES;
	textField.autocorrectionType											= UITextAutocorrectionTypeNo;
	textField.autocapitalizationType										= UITextAutocapitalizationTypeNone;
	textField.clearButtonMode												= indexPath.row == 1 ? UITextFieldViewModeNever : UITextFieldViewModeWhileEditing;
	textField.delegate														= self;
	theCell.accessoryView													= textField;
	theCell.selectionStyle													= UITableViewCellSelectionStyleNone;
	theCell.accessoryType													= UITableViewCellAccessoryNone;
	
	return theCell;
}

//
// select row
//
- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
	UITableViewCell* theCell												= [tableView cellForRowAtIndexPath:indexPath];
	[theCell.accessoryView becomeFirstResponder];
}

//
// cancel edit
//
- (void)cancelEdit:(id)sender
{
	[self.tableView endEditing:YES];
	if(self.callback)
		self.callback(NO, nil);
}

//
// commit edit
//
- (void)commitEdit:(id)sender
{
	[self.tableView endEditing:YES];
	NSString* message														= nil;
	if(!self.serverInfo.serverName.length)
		message																= NSLocalizedStringFromTable(@"Please input server name", @"ServerDetailsViewController", nil);
	else if(!self.serverInfo.serverType.length)
		message																= NSLocalizedStringFromTable(@"Please select a server type", @"ServerDetailsViewController", nil);
	else if(!self.serverInfo.serverAddress.length)
		message																= NSLocalizedStringFromTable(@"Please input server address", @"ServerDetailsViewController", nil);

	if(message)
		[UIAlertView alertViewWithTitle:NSLocalizedString(@"Error", nil) message:message cancel:NSLocalizedString(@"OK", nil)];
	else if(self.callback)
		self.callback(YES, self.serverInfo);
}

//
// show picker view
//
- (void)showServerTypePicker:(id)sender
{
	UIPickerView* pickerView												= [[[UIPickerView alloc] init] autorelease];
	[pickerView sizeToFit];
	pickerView.autoresizingMask												= UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
	pickerView.delegate														= self;
	pickerView.dataSource													= self;
	pickerView.showsSelectionIndicator										= YES;
	UITableViewCell* theCell												= [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
	UITextField* textField													= (UITextField*)theCell.accessoryView;
	textField.inputView														= pickerView;
	
	UIToolbar* doneButtonView												= [[[UIToolbar alloc] init] autorelease];
	doneButtonView.barStyle													= UIBarStyleBlack;
	doneButtonView.translucent												= YES;
	doneButtonView.tintColor												= nil;
	[doneButtonView sizeToFit];
	
	UIBarButtonItem* flexSpace												= [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease];
	UIBarButtonItem* doneButton												= [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(selectServerType:)] autorelease];
	[doneButtonView setItems:[NSArray arrayWithObjects:flexSpace, doneButton, nil]];
	textField.inputAccessoryView											= doneButtonView;
	
	[pickerView reloadAllComponents];
	[pickerView selectRow:[self.serverInfo.serverType isEqualToString:@"rtorrent-xmlrpc"] ? 0 : 1 inComponent:0 animated:NO];
}

//
// select server type
//
- (void)selectServerType:(id)sender
{
	UITableViewCell* theCell												= [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
	UITextField* textField													= (UITextField*)theCell.accessoryView;
	[self textFieldShouldReturn:textField];
}

//
// components
//
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView
{
	return 1;
}

//
// items
//
- (NSInteger)pickerView:(UIPickerView*)pickerView numberOfRowsInComponent:(NSInteger)component
{
	return 2;
}

//
// title
//
- (NSString*)pickerView:(UIPickerView*)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
	return row ? @"deluge-web-jsonrpc" : @"rtorrent-xmlrpc";
}

//
// select row
//
- (void)pickerView:(UIPickerView*)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
	UITableViewCell* theCell												= [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
	UITextField* textField													= (UITextField*)theCell.accessoryView;
	textField.text															= [self pickerView:pickerView titleForRow:row forComponent:component];
}

//
// disable server type change
//
- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string
{
	return textField.tag == 1 ? NO : YES;
}

//
// begin editing
//
- (void)textFieldDidBeginEditing:(UITextField*)textField
{
	[self showServerTypePicker:textField];
}

//
// end editing
//
- (void)textFieldDidEndEditing:(UITextField*)textField
{
	switch(textField.tag)
	{
	case 0:
		self.serverInfo.serverName											= textField.text;
		break;
		
	case 1:
		self.serverInfo.serverType											= textField.text;
		break;
		
	case 2:
		self.serverInfo.serverAddress										= textField.text;
		break;
		
	case 3:
		self.serverInfo.userName											= textField.text;
		break;
		
	case 4:
		self.serverInfo.password											= textField.text;
		break;
	}
}

//
// return
//
- (BOOL)textFieldShouldReturn:(UITextField*)textField
{
	switch(textField.tag)
	{
	case 0:
	case 1:
	case 2:
	case 3:
		[[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:textField.tag + 1 inSection:0]].accessoryView becomeFirstResponder];
		break;
		
	case 4:
		[self commitEdit:textField];
		break;
	}
	return YES;
}

@end